/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.assertj.ohos.api.ability;

import ohos.aafwk.ability.Ability;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import org.assertj.ohos.api.content.AbstractContextAssert;

import java.io.IOException;

import static org.assertj.core.api.Assertions.assertThat;

/** Assert */
public abstract class AbilityAssert<S extends AbilityAssert<S, A>, A extends Ability> extends AbstractContextAssert<S, A> {
    public AbilityAssert(A actual, Class<S> selfType) {
    super(actual, selfType);
    }

    /**
    * 有原始设备id
    *
    * @param deviceId 设备id
    * @return S
    */
    public S hasOriginalDeviceId(String deviceId) {
    isNotNull();
    int actualDeviceId = actual.getDisplayOrientation();
    assertThat(actualDeviceId) //
        .overridingErrorMessage("Expected deviceId <%s> but was <%s>.", actualDeviceId, deviceId)
        .isEqualTo(deviceId);
    return myself;
    }

    /**
     * 有标题
     *
     * @param title 标题
     * @return S
     */
    public S hasTitle(CharSequence title) {
    isNotNull();
    CharSequence actualTitle = actual.getAbilityInfo().getLabel();
    assertThat(actualTitle) //
        .overridingErrorMessage("Expected title <%s> but was <%s>.", title, actualTitle) //
        .isEqualTo(title);
    return myself;
    }

    /**
     * 有标题
     *
     * @param resId 资源id
     * @return S
     * @throws NotExistException  不存在异常
     * @throws WrongTypeException 错误类型异常
     * @throws IOException IO异常
     */
    public S hasTitle(int resId) throws NotExistException, WrongTypeException, IOException {
    isNotNull();
    return hasTitle(actual.getResourceManager().getElement(resId).getString(resId));
    }

    /**
     * 有窗口焦点
     *
     * @return S
     */
    public S hasWindowFocus() {
    isNotNull();
    assertThat(actual.hasWindowFocus()) //
        .overridingErrorMessage("Expected to have focus but was not focused.") //
        .isTrue();
    return myself;
    }

    /**
     * 变更配置
     *
     * @return S
     */
    public S isChangingConfigurations() {
    isNotNull();
    assertThat(actual.isUpdatingConfigurations()) //
        .overridingErrorMessage("Expected changing configurations but was not changing.") //
        .isTrue();
    return myself;
    }

    /**
     * 没有变更配置
     *
     * @return S
     */
    public S isNotChangingConfigurations() {
    isNotNull();
    assertThat(actual.isUpdatingConfigurations()) //
        .overridingErrorMessage("Expected not to be changing configurations but was changing.") //
        .isFalse();
    return myself;
    }

    /**
     * 被销毁
     *
     * @return S
     */
    public S isDestroyed() {
    isNotNull();
    assertThat(actual.isTerminating()) //
        .overridingErrorMessage("Expected to be destroyed but was not destroyed.") //
        .isTrue();
    return myself;
    }

    /**
     * 没有被销毁
     *
     * @return S
     */
    public S isNotDestroyed() {
    isNotNull();
    assertThat(actual.isTerminating()) //
        .overridingErrorMessage("Expected not to be destroyed but was destroyed.") //
        .isFalse();
    return myself;
    }

    /**
     * 完成
     *
     * @return S
     */
    public S isFinishing() {
    isNotNull();
    assertThat(actual.isTerminating()) //
        .overridingErrorMessage("Expected to be finishing but was not finishing.") //
        .isTrue();
    return myself;
    }

    /**
     * 没有完成
     *
     * @return S
     */
    public S isNotFinishing() {
    isNotNull();
    assertThat(actual.isTerminating()) //
        .overridingErrorMessage("Expected to not be finishing but was finishing.") //
        .isFalse();
    return myself;
    }

}
